package com.hzit.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.hzit.manager.entity.ItemEntity;
import com.hzit.manager.service.ItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzit.common.utils.PageUtils;
import com.hzit.common.utils.Query;

import com.hzit.manager.dao.GoodsDao;
import com.hzit.manager.entity.GoodsEntity;
import com.hzit.manager.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
	@Autowired
	private ItemService itemService;
	@Autowired
	private RabbitTemplate rabbitTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public void updateStatus(String id, String status) {
		//1. 将商品审核通过
		GoodsEntity goods = new GoodsEntity();
		goods.setId(id);
		goods.setAuditStatus(status);
		this.updateById(goods);

		System.out.println("正在向hzit-search模块发送消息！");
		//2. 如果上面的商品审核成功，就发送消息（内容就是这个spu商品id关联的sku商品列表）
		List<ItemEntity> itemList = itemService.list(new QueryWrapper<ItemEntity>().eq("goods_id", id).eq("status","1"));

		//3. 使用rabbitmq发送消息
		rabbitTemplate.convertAndSend("updateStatus", JSON.toJSONString(itemList));

	}

}